var allpoint=null;
var dDrawLineList=null;
var voltage_type=null;
var gt_type=null;
var gt_material=null;
var dCrossCostList=null;
var map;
var infoWindow ;
$(function () {
    $.when( $.get(baseURL + 'bss/dpoint/select?pGuid='+ GetUrlParam("guid"), function (r) {
        allpoint = r.returnlist;
     }),
         $.get(baseURL + 'bss/ddrawline/select?pGuid='+ GetUrlParam("guid"), function (r) {
             dDrawLineList = r.returnlist;
            }),
        $.get(baseURL + 'bss/dcrosscost/select?pGuid='+ GetUrlParam("guid"), function (r) {
            dCrossCostList = r.returnlist;
        }),

        getdatadictionary("voltage_type"),
        getdatadictionary("gt_type"),
        getdatadictionary("gt_material")

	).done(
    function () {
        var overlayGroup= new AMap.OverlayGroup();
        var lxGroup= new AMap.OverlayGroup();
        var djGroup= new AMap.OverlayGroup();
        var longitude= GetUrlParam("longitude");
        var latitude= GetUrlParam("latitude");
         if(longitude==null||longitude=="")
		 {
             longitude=106.941042
		 }
        if(latitude==null||latitude=="")
        {
            latitude=27.707006
        }
        map = new AMap.Map("mapContainer", {
            resizeEnable: true,
            expandZoomRange:true,
            zoom:10,
            zooms:[3,20],
            viewMode: '2D',
            center: [longitude, latitude]
        });
        AMap.plugin(['AMap.Scale','AMap.MapType'],function(){

            map.addControl(new AMap.Scale());
            map.addControl(new AMap.MapType());
        })

        infoWindow  = new AMap.InfoWindow({
            isCustom: true,  //使用自定义窗体
            content: "",
            offset: new AMap.Pixel(16, -45)
        });

        for ( d in  allpoint) {


        	if(d==0) {
                map.setZoomAndCenter(18, [allpoint[d].pPInputlongitude, allpoint[d].pPInputlatitude]);
            }
            var imgurl="";


            var content = [];

            content.push("点位类型："+allpoint[d].pPName);
            content.push("点位编号："+allpoint[d].pPCode);

            var str1 ="javascript:void(0)";
            var str2 ="showHtml('"+allpoint[d].guid+"','"+allpoint[d].pGuid+"')";
            var str ="<a href="+str1+"; onclick="+str2+">详细信息</a>";
            content.push(str);


            if(allpoint[d].pPName=="杆塔")
            {
                imgurl="/imgs/gt.png";

            }
            if(allpoint[d].pPName=="变压器")
            {
                imgurl="/imgs/byq.png";

            }
            if(allpoint[d].pPName=="箱式变电站")
            {
                imgurl="/imgs/xsbdz.png";
            }
            if(allpoint[d].pPName=="开关柜")
            {
                imgurl="/imgs/zykgg.png";
            }
            if(allpoint[d].pPName=="柱上开关")
            {
                imgurl="/imgs/zskg.png";
            }
            if(allpoint[d].pPName=="无功补偿")
            {
                imgurl="/imgs/wgbc.png";
            }
            if(allpoint[d].pPName=="低压开关柜")
            {
                imgurl="/imgs/dykgg.png";
            }
            if(allpoint[d].pPName=="电缆分支箱")
            {
                imgurl="/imgs/dlfzx.png";
            }
            if(allpoint[d].pPName=="下户点")
            {
                imgurl="/imgs/xhd.png";
            }
            if(allpoint[d].pPName=="搭火点"||allpoint[d].pPName=="搭火点2")
            {
                imgurl="/imgs/dhd.png";
            }
            if(allpoint[d].pPName=="表箱")
            {
                imgurl="/imgs/bx.png";
            }
            if(allpoint[d].pPName=="柱上变压器")
            {
                imgurl="/imgs/zsbyq.png";
            }
            if(allpoint[d].pPName=="电缆井")
            {
                imgurl="/imgs/dlj.png";
            }
            if(allpoint[d].pPName=="墙担点")
            {
                imgurl="/imgs/qzj.png";
            }
          var  marker = new AMap.Marker({
                map: map,
                position: [allpoint[d].pPInputlongitude, allpoint[d].pPInputlatitude],
              icon: new AMap.Icon({
                  image: imgurl,
                  size: new AMap.Size(30, 30),  //图标大小
                  imageSize: new AMap.Size(30, 30)
              }),
              offset: new AMap.Pixel(-15, -30),

              label: {
                    content: allpoint[d].pPCode,
                    offset: new AMap.Pixel(20, 40)//修改label相对于maker的位置
                },
               extData:allpoint[d]


            });





            marker.extData = {
                guid:allpoint[d].guid,
                pGuid:allpoint[d].pGuid,
                pPParentguid:allpoint[d].pPParentguid,
                pPType:allpoint[d].pPType,
                pPCode:allpoint[d].pPCode,
                content:content
            };


         //  marker.on("dblclick", showHtml);
           marker.on('click', markerClick);
          // marker.emit('click', {target: marker});
            for ( m in  dDrawLineList) {
                if(dDrawLineList[m].pPGuid==allpoint[d].guid)
                {
                    var  markerla = new AMap.Marker({
                        position: [dDrawLineList[m].dLLongitude, dDrawLineList[m].dLLatitude],
                        icon: new AMap.Icon({
                            image: "/imgs/lx.png",
                            size: new AMap.Size(20, 20),  //图标大小
                            imageSize: new AMap.Size(20,20)
                        }),
                        offset: new AMap.Pixel(-10, -20)
                   })

                    lxGroup.add(markerla);
                    var pathla = [
                        new AMap.LngLat(allpoint[d].pPInputlongitude, allpoint[d].pPInputlatitude),
                        new AMap.LngLat(dDrawLineList[m].dLLongitude, dDrawLineList[m].dLLatitude),
                    ];
                    var polylinela = new AMap.Polyline({
                        path: pathla,
                        showDir:true,
                        borderWeight: 1, // 线条宽度，默认为 1
                        strokeColor: 'blue', // 线条颜色
                        lineJoin: 'round' // 折线拐点连接处样式
                    });
                    lxGroup.add(polylinela);

                }
            }

            if(allpoint[d].pPUpguid!=null) {

                for ( dd in  allpoint)
				{
					if(allpoint[d].pPUpguid==allpoint[dd].guid)
					{
                        var path = [
                            new AMap.LngLat(allpoint[d].pPInputlongitude, allpoint[d].pPInputlatitude),
                            new AMap.LngLat(allpoint[dd].pPInputlongitude, allpoint[dd].pPInputlatitude),
                         ];
                        var polyline = new AMap.Polyline({
                            path: path,
                            showDir:true,
                            borderWeight: 1, // 线条宽度，默认为 1
                            strokeColor: 'red', // 线条颜色
                            lineJoin: 'round' // 折线拐点连接处样式
                        });
                        map.add(polyline);

                        var text = new AMap.Text({
                            text:allpoint[d].pPInputspan,
                            textAlign:'center', // 'left' 'right', 'center',
                            verticalAlign:'middle', //middle 、bottom
                            draggable:true,
                            cursor:'pointer',
                             position: [(allpoint[d].pPInputlongitude+allpoint[dd].pPInputlongitude)/2,(allpoint[d].pPInputlatitude+allpoint[dd].pPInputlatitude)/2],
                            offset: new AMap.Pixel(-10, -10)
                        });
                      ;
                        djGroup.add(text);

					}
				}


            }



         }

        for ( n in  dCrossCostList) {
            var  markerjk = new AMap.Marker({
                position: [dCrossCostList[n].cCInputlongitude, dCrossCostList[n].cCInputlatitude],
                icon: new AMap.Icon({
                    image: "/imgs/p_jcky.png",
                    size: new AMap.Size(20, 20),  //图标大小
                    imageSize: new AMap.Size(20,20)
                }),
                offset: new AMap.Pixel(-10, -20)
            })
            overlayGroup.add(markerjk);

        }


        map.add(lxGroup);
        vm.lxGroup=lxGroup;
        map.add(overlayGroup);
        vm.overlayGroup=overlayGroup;
        map.add(djGroup);
        vm.djGroup=djGroup;
        // map.addControl(new AMap.ToolBar());
        // if (AMap.UA.mobile) {
        //     document.getElementById('button_group').style.display = 'none';
        // }
    })




});

function showPhotos(images) {
    parent.showPhotos(images)

}
function markerClick(e) {

   infoWindow.setContent(createInfoWindow(e.target.extData.pPCode,  e.target.extData.content.join("<br/>")));
   infoWindow.open(map, e.target.getPosition());
}

function createInfoWindow(title, content) {
    var info = document.createElement("div");
    info.className = "custom-info input-card content-window-card";

    //可以通过下面的方式修改自定义窗体的宽高
    //info.style.width = "400px";
    // 定义顶部标题
    var top = document.createElement("div");
    var titleD = document.createElement("div");
    var closeX = document.createElement("img");
    top.className = "info-top";
    titleD.innerHTML = title;
    closeX.src = "https://webapi.amap.com/images/close2.gif";
    closeX.onclick = closeInfoWindow;

    top.appendChild(titleD);
    top.appendChild(closeX);
    info.appendChild(top);

    // 定义中部内容
    var middle = document.createElement("div");
    middle.className = "info-middle";
    middle.style.backgroundColor = 'white';
    middle.innerHTML = content;
    info.appendChild(middle);

    // 定义底部内容
    var bottom = document.createElement("div");
    bottom.className = "info-bottom";
    bottom.style.position = 'relative';
    bottom.style.top = '0px';
    bottom.style.margin = '0 auto';
    var sharp = document.createElement("img");
    sharp.src = "https://webapi.amap.com/images/sharp.png";
    bottom.appendChild(sharp);
    info.appendChild(bottom);
    return info;
}

//关闭信息窗体
function closeInfoWindow() {
    map.clearInfoWindow();
}

function showHtml(guid,pGuid) {

    // var guid = e.target.extData.guid;
    // var pGuid = e.target.extData.pGuid;
    // var pPParentguid = e.target.extData.pPParentguid;
    // var pPType = e.target.extData.pPType;
    // var pPCode = e.target.extData.pPCode;

    var content = "apbaseothorcj212.html?guid="+guid+"&pGuid="+pGuid;

    layer.open({
        type: 2,
        title: false,

        area: ['95%', '95%'],
        content: content

    });

}



function getdatadictionary(sss)
{

    $.get(baseURL + "api/bdatadictionary/select?d_code="+sss, function(r){
        if(sss=="voltage_type")
        {
            voltage_type=r.returnlist;
        }
        if(sss=="gt_type")
        {
            gt_type=r.returnlist;
        }
        if(sss=="gt_material")
        {
            gt_material=r.returnlist;
        }

        vm.voltage_type=voltage_type;
        vm.gt_type=gt_type ;
		vm.gt_material=gt_material;


    });
}
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        voltage_type:voltage_type,
        gt_type:gt_type,
        gt_material:gt_material,
        overlayGroup:null,
        jcky:"ok",
        djGroup:null,
        dj:"ok",
        kxGroup:null,
        lx:"ok",
        dCrossCostList: {},
		dPoint: {},
        dDrawLineList:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dPoint = {};
		},
		update: function (e) {
            alert(e.target.F.extData);
			if(guid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(guid)
		},
		saveOrUpdate: function (event) {
			var url = vm.dPoint.guid == null ? "bss/dpoint/save" : "bss/dpoint/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.dPoint),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "bss/dpoint/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "bss/dpoint/info/"+guid, function(r){
                vm.dPoint = r.dPoint;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
        reloadjcky: function (event) {
		    if(vm.jcky=="ok")
            {
                vm.overlayGroup.hide();
                vm.jcky="no"
            }
            else
            {
                vm.overlayGroup.show();
                vm.jcky="ok"
            }


        },
        reloadlx: function (event) {
            if(vm.lx=="ok")
            {
                vm.lxGroup.hide();
                vm.lx="no"
            }
            else
            {
                vm.lxGroup.show();
                vm.lx="ok"
            }


        },
        reloaddj: function (event) {
            if(vm.dj=="ok")
            {
                vm.djGroup.hide();
                vm.dj="no"
            }
            else
            {
                vm.djGroup.show();
                vm.dj="ok"
            }


        }

	}
});